package com.muqi.app.qlearn.student.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.adapter.CommonAdapter;
import com.muqi.app.qlearn.adapter.CommonViewHolder;
import com.muqi.app.qlearn.modles.Examination;
import com.muqi.app.qlearn.modles.Resolve;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.R.id;
import com.muqi.app.qlearn.student.R.layout;
import com.muqi.app.qlearn.student.org.TeacherInfoActivity;
import com.muqi.app.user.widget.CircularImageView;
import com.muqi.app.widget.pulltorefresh2.XListView;
import com.muqi.app.widget.pulltorefresh2.XListView.IXListViewListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ResolveActivity extends BaseFragmentActivity implements
		IXListViewListener {

	private XListView listView;
	private TextView headContent;
	private CircularImageView headAva;
	private TextView headName;

	private TextView itemName;
	private TextView itemOrgName;
	private TextView itemTime;
	private TextView itemConsult;
	private TextView itemAnswer;
	private CircularImageView itemAvator;

	private List<Resolve> listData = new ArrayList<Resolve>();
	private int page = 1;
	private Examination examination;
	private String eId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_resolve);
	}

	@Override
	protected void onInit() {
		listView = (XListView) findViewById(R.id.resolve_lv);

		View headView = getLayoutInflater().inflate(R.layout.header_resolve,
				null);
		headContent = (TextView) headView.findViewById(R.id.item_content);
		headAva = (CircularImageView) headView.findViewById(R.id.item_avator);
		headName = (TextView) headView.findViewById(R.id.head_name);

		listView.addHeaderView(headView);
		listView.setPullLoadEnable(false);
		listView.setPullRefreshEnable(true);
		listView.setXListViewListener(this);
		listView.setAdapter(adapter);

		examination = (Examination) getIntent().getSerializableExtra("resolve");
		eId = getIntent().getStringExtra("id");

		if (examination == null && eId == null) {
			finish();
		}
		if (examination != null) {
			eId=examination.id;
			showView();

		} else if (eId != null) {
			loadExamination();
		}

		
	}

	/**
	 * 加载试题数据
	 */
	private void loadExamination() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("token", mSpUtil.getToken());
			sendMap.put("student_id", mSpUtil.getCurrentChildId());
			sendMap.put("class_manager_id", mSpUtil.getChildClassId());
			sendMap.put("q_id", eId);
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.GET_EXAMINATION_API, sendMap);
			MyAsyncHttp.get(this, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					
					if (resuleCode == RESULTCODE.SUCCESS) {
						examination = ResponseUtils
								.getExaminationInfo(mContext, result);
						if(examination!=null){
							showView();
						}
						
						
					}
				}
			});
		} else {
			hideLoading();
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}


	}

	/**
	 * 展示数据
	 */
	private void showView() {
		headContent.setText(examination.q_name);
		headName.setText(examination.type);

	}

	@Override
	protected void onData() {
		loadResolveData();

	}

	/**
	 * 加载试题数据
	 */
	private void loadResolveData() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("token", mSpUtil.getToken());
			sendMap.put("q_id", eId);
			sendMap.put("page", page);
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.GET_RESOLVE_API, sendMap);
			MyAsyncHttp.get(this, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					onLoadView();
					hideLoading();
					if (resuleCode == RESULTCODE.SUCCESS) {
						List<Resolve> getLists = ResponseUtils
								.getResolveInfos(mContext, result);
						listData.add(new Resolve());
						listData.add(new Resolve());
						adapter.notifyDataSetChanged();
						if (getLists != null && getLists.size() > 0) {
							if (page == 1) {

								listData.clear();

							}
							for (Resolve resolve : getLists) {
								listData.add(resolve);
							}
							
							adapter.notifyDataSetChanged();

							if (getLists.size() < 10) {
								listView.setPullLoadEnable(false);
							} else {
								listView.setPullLoadEnable(true);
								page++;
							}
						}
					}
				}
			});
		} else {
			hideLoading();
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}
	}

	private CommonAdapter<Resolve> adapter = new CommonAdapter<Resolve>(this,
			listData, R.layout.item_resolve) {

		@Override
		protected void convertView(View item, final Resolve t) {
			if (t != null) {
				itemName = CommonViewHolder.get(item, R.id.item_name);
				itemOrgName = CommonViewHolder.get(item, R.id.item_orgName);
				itemTime = CommonViewHolder.get(item, R.id.item_time);
				itemConsult = CommonViewHolder.get(item, R.id.item_consult);
				itemAvator = CommonViewHolder.get(item, R.id.item_avator);
				itemAnswer= CommonViewHolder.get(item, R.id.item_answer);
				itemName.setText(t.name);
				itemOrgName.setText(t.instructor_name);
				itemTime.setText(t.create_time);
				itemAnswer.setText(t.content);
				itemConsult.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent(mContext,
								TeacherInfoActivity.class);
						intent.putExtra("userid", t.reply_to_user_id);
						startActivity(intent);
					}
				});
			}

		}
	};

	public void onBack(View v) {
		finish();
	}

	@Override
	public void onRefresh() {
		page = 1;
		loadResolveData();

	}

	@Override
	public void onLoadMore() {
		loadResolveData();

	}
	private void onLoadView() {
		listView.stopRefresh();
		listView.stopLoadMore();
		listView.setRefreshTime("刚刚");
	}
}